/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kifas.service;

import com.kifas.model.xml.AkunMasterModel;
import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.CustomerModel;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.JenisBarangModel;
import com.kifas.model.xml.JenisCustomerModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.ReferenceNumberingModel;
import com.kifas.model.xml.SalesModel;
import com.kifas.model.xml.SatuanModel;
import com.kifas.model.xml.SupplierModel;
import com.kifas.ui.util.ServiceConstants;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;

/**
 *
 * @author dhamarsu
 */
public class MasterService {
    private static Logger log = Logger.getLogger(MasterService.class);
    private String serviceUrl  = ServiceConstants.URL_CONTROLLER;
    private Client restClient;

    public MasterService() {
        restClient = Client.create();

        
    }

    public ReferenceNumberingModel getReferenceNumber(Integer unit, String prefix) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_REFERENCE_NUMBER);
        Form params = new Form();
        params.add("unit", unit);
        params.add("prefix", prefix);
        return webResource.queryParams(params).get(ReferenceNumberingModel.class);
    }

    public PersistStatus setJenisCustomer(JenisCustomerModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_CUSTOMER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putJenisCustomer(JenisCustomerModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_CUSTOMER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<JenisCustomerModel> getJenisCustomers(){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_CUSTOMER);
        return webResource.get(new GenericType<List<JenisCustomerModel>>(){});
    }

    public List<CustomerModel> getCustomers(){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_CUSTOMER);
        return webResource.get(new GenericType<List<CustomerModel>>(){});
    }

    public CustomerModel getCustomer(CustomerModel cm){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_CUSTOMER + cm.getIdCustomer());
        return webResource.get(CustomerModel.class);        
    }

    public PersistStatus setCustomer(CustomerModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_CUSTOMER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putCustomer(CustomerModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_CUSTOMER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<SalesModel> getSaless() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SALES);
        return webResource.get(new GenericType<List<SalesModel>>(){});
    }

    public SalesModel getSales(SalesModel sm){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SALES + sm.getIdSales());
        return webResource.get(SalesModel.class);
    }

    public List<SupplierModel> getSupplierModels() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SUPPLIER);
        return webResource.get(new GenericType<List<SupplierModel>>(){});
    }

    public PersistStatus setSupplier(SupplierModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SUPPLIER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putSupplier(SupplierModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SUPPLIER);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<SatuanModel> getSatuans() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SATUAN);
        return webResource.get(new GenericType<List<SatuanModel>>(){});
    }

    public PersistStatus setSatuan(SatuanModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SATUAN);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putSatuan(SatuanModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_SATUAN);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<JenisBarangModel> getJenisBarangs() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_BARANG);
        return webResource.get(new GenericType<List<JenisBarangModel>>(){});
    }

    public PersistStatus setJenisBarang(JenisBarangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_BARANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putJenisBarang(JenisBarangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_JENIS_BARANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<BarangModel> getBarangs() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_BARANG);
        return webResource.get(new GenericType<List<BarangModel>>(){});        
    }

    public PersistStatus setBarang(BarangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_BARANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putBarang(BarangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_BARANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public SatuanModel getSatuansByBarang(String id_barang) {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_BARANG + "/" + id_barang);
        BarangModel barangModel = webResource.get(BarangModel.class);
        return barangModel.getSatuan();
    }

    public List<GudangModel> getGudangs() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_GUDANG);
        return webResource.get(new GenericType<List<GudangModel>>(){});
    }

    public PersistStatus setGudang(GudangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_GUDANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).post(PersistStatus.class, m);
        return  persistStatus;
    }

    public PersistStatus putGudang(GudangModel m){
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_GUDANG);
        PersistStatus persistStatus = webResource.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML).put(PersistStatus.class, m);
        return  persistStatus;
    }

    public List<AkunMasterModel> getMasterAkuns() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_AKUN);
        return webResource.get(new GenericType<List<AkunMasterModel>>(){});
    }

    public List<AkunMasterModel> getMasterAkunsLeaf() {
        WebResource webResource = restClient.resource(serviceUrl + ServiceConstants.URL_AKUN_LEAF);
        return webResource.get(new GenericType<List<AkunMasterModel>>(){});
    }
    
}
